// pages/user/goodsDetail/goodsDetail.ts

import {
  goodsDetail
} from "../../../http/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    id: "",
    isInitialPage:false,
    goodsObj: {
      id: "",
      name: "",
      image: "",
      price: "",
    }
  },
  handleToPay(e){
    wx.setStorageSync('cart',[])
    const cartArr = []
    const goodsObj = e.currentTarget.dataset.item;
    const num = e.currentTarget.dataset.num;
    goodsObj.num = parseInt(num)
    cartArr.push(goodsObj)
    wx.setStorageSync('cart',cartArr)
    wx.navigateTo({
      url: '/pages/user/takeoutOrderConfirm/orderConfirm',
    })
  },
  handleAddCart(e) {
    let goodsObj = {}
    let num = 0

    if (e.type === "tap") {
      goodsObj = e.currentTarget.dataset.item;
      num = e.currentTarget.dataset.num;
    } else {
      goodsObj = e.detail.currentTarget.dataset.item;
      num = e.detail.currentTarget.dataset.num;
    }

    const cartArr = wx.getStorageSync('cart') || [];
  
    let goodsIndex = "";

    const arr = cartArr.filter((item, index) => {
      if (item.id === goodsObj.id) {
        goodsIndex = index
      }
      return item.id === goodsObj.id
    })

    if (arr.length > 0) {

      const newNum = parseInt(cartArr[goodsIndex].num) + parseInt(num)
      cartArr[goodsIndex].num = newNum
    
    } else {
      goodsObj.num = parseInt(num)
      cartArr.push(goodsObj)
    }

    wx.showToast({
      title: '已添加至购物车',
      icon: 'none'
    })
    wx.setStorageSync('cart', cartArr)
    setTimeout(() => {
      
      wx.navigateBack()
    }, 1000)

 



  },

  handleCloseDetail() {
    this.triggerEvent('handleCloseDetail')
  },
  handleChange(e) {
    const num = e.detail
    this.setData({
      num
    })
  },



  getData() {
    



    wx.showLoading()
    goodsDetail(this.data.id).then(res => {
      if (res.data.code === 200) {
        this.setData({
          goodsObj: res.data.data[0]
        })
      }

    }).finally(() => {
      wx.hideLoading()
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {



    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getData()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const pages = getCurrentPages();
    if(pages.length === 0){
      this.setData({
        isInitialPage:true
      })

    }
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

    return {
      title:`${this.data.goodsObj.name}`,
      imageUrl: `${this.data.goodsObj.image}`,//转发携带的图片,可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG
      path:`/pages/user/goodsShare/goodsShare?id=${this.data.id}`,//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }

  }
})